﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace GenUI.WPF.GenControls
{
    internal class TDateTimeView : Control
    {
        public Action<string> DateTimeOK;
        Button btnhh;
        Button btnmm;
        Button btnss;
        Button btnOK;
        Button btnNow;
        Button btnClose;
        Calendar calendar;
        Popup popChioce;
        public string formerHourStr = string.Empty;
        static TDateTimeView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TDateTimeView), new FrameworkPropertyMetadata(typeof(TDateTimeView)));
        }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            btnhh = GetTemplateChild("PART_btnhh") as Button;
            btnmm = GetTemplateChild("PART_btnmm") as Button;
            btnss = GetTemplateChild("PART_btnss") as Button;
            btnOK = GetTemplateChild("PART_btnOK") as Button;
            btnNow = GetTemplateChild("PART_btnNow") as Button;
            btnClose = GetTemplateChild("PART_btnClose") as Button;
            calendar = GetTemplateChild("PART_calendar") as Calendar;
            popChioce = GetTemplateChild("PART_popChioce") as Popup;

            btnhh.Click += Btnhh_Click;
            btnmm.Click += Btnmm_Click;
            btnss.Click += Btnss_Click;
            btnOK.Click += BtnOK_Click;
            btnNow.Click += BtnNow_Click;
            calendar.PreviewMouseUp += Calendar_PreviewMouseUp;
        }
        private void BtnNow_Click(object sender, RoutedEventArgs e)
        {
            popChioce.IsOpen = false;//THourView 或 TMinSexView 所在pop 的关闭动作

            if (btnNow.Content.ToString() == "零点")
            {
                btnhh.Content = "00";
                btnmm.Content = "00";
                btnss.Content = "00";
                btnNow.Content = "当前";
                btnNow.Background = System.Windows.Media.Brushes.LightBlue;
            }
            else
            {
                DateTime dt = DateTime.Now;
                btnhh.Content = dt.Hour.ToString().PadLeft(2, '0');
                btnmm.Content = dt.Minute.ToString().PadLeft(2, '0');
                btnss.Content = dt.Second.ToString().PadLeft(2, '0');
                btnNow.Content = "零点";
                btnNow.Background = System.Windows.Media.Brushes.LightGreen;
            }
        }
        private void BtnOK_Click(object sender, RoutedEventArgs e)
        {
            DateTime? dt = new DateTime?();

            if (calendar.SelectedDate == null)
            {
                dt = DateTime.Now.Date;
            }
            else
            {
                dt = calendar.SelectedDate;
            }

            DateTime dtCal = Convert.ToDateTime(dt);
            string timeStr = btnhh.Content + ":" + btnmm.Content + ":" + btnss.Content;

            string dateStr;
            dateStr = dtCal.ToString("yyyy-MM-dd");

            string dateTimeStr;
            dateTimeStr = dateStr + " " + timeStr;
            ;
            string str1 = dateTimeStr;
            OnDateTimeContent(str1);
        }
        protected void OnDateTimeContent(string dateTimeStr)
        {
            DateTimeOK?.Invoke(dateTimeStr);
        }
        private void Calendar_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (Mouse.Captured is CalendarItem)
            {
                Mouse.Capture(null);
            }
        }
        private void Btnss_Click(object sender, RoutedEventArgs e)
        {
            if (popChioce.IsOpen == true)
            {
                popChioce.IsOpen = false;
            }

            //秒钟 跟分钟 都是60，所有秒钟共用 分钟的窗体即可
            TMinSexView sexView = new TMinSexView
            {
                formerMinStr = btnhh.Content.ToString(),
                Title = "秒   钟"//修改 TMinSexView 的标题名称为秒钟
            };//TMinSexView 构造函数 传入秒钟数据
            sexView.MinClick += (sexStr) => //TMinSexView 中 所选择确定的 秒钟数据 的传递动作
            {
                btnss.Content = sexStr;
                popChioce.IsOpen = false;//TMinSexView 所在的 pop 关闭动作
            };

            popChioce.Child = sexView;
            popChioce.IsOpen = true;
        }
        private void Btnmm_Click(object sender, RoutedEventArgs e)
        {
            if (popChioce.IsOpen == true)
            {
                popChioce.IsOpen = false;
            }

            TMinSexView minView = new TMinSexView() { formerMinStr = btnhh.Content.ToString() };//TMinSexView 构造函数传递 分钟数据
            minView.MinClick += (minStr) => //TMinSexView 中 点击选择的分钟数据的 传递动作
            {

                btnmm.Content = minStr;
                popChioce.IsOpen = false;//TMinSexView 所在的 pop 关闭动作
            };

            popChioce.Child = minView;
            popChioce.IsOpen = true;
        }
        private void Btnhh_Click(object sender, RoutedEventArgs e)
        {
            if (popChioce.IsOpen == true)
            {
                popChioce.IsOpen = false;
            }

            THourView hourView = new THourView() { formerHourStr = btnhh.Content.ToString() };// THourView 构造函数传递小时数据
            hourView.HourClick += (hourstr) => //THourView 点击所选小时后的 传递动作
            {

                btnhh.Content = hourstr;
                popChioce.IsOpen = false;//THourView 所在pop 的关闭动作
            };

            popChioce.Child = hourView;
            popChioce.IsOpen = true;
        }
    }
}
